package com.wh.service.impl;

import com.wh.dao.BaseDao;
import com.wh.dao.ext.SysRolePowerDao;
import com.wh.model.BaseModel;
import com.wh.model.SysRolePower;
import com.wh.service.IExt.ISysRolePowerService;
import com.legend.utils.*;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:2014-02-14 13:15:16
*/
public class SysRolePowerService extends BaseServiceImp implements ISysRolePowerService {

    Logger log = Logger.getRootLogger();

    private SysRolePowerService() {
    }

    //volatile修饰的写变量不能和之前的读写代码调整，读变量不能和之后的读写代码调整
    private volatile static ISysRolePowerService instance = null;

    /**
     * 单例
     *
     * @return
     */
    public static ISysRolePowerService getInstance() {
        if (instance == null) {
            synchronized (ISysRolePowerService.class) {
                if (instance == null) {
                    instance = new SysRolePowerService();
                }
            }
        }
        return instance;
    }

    /**
     * 批量删除数据
     */

    public ReturnResult delByIds(String ids) {
        SqlSession session = getSqlSession();
        ReturnResult result = new ReturnResult();
        try {
            SysRolePowerDao dao = session.getMapper(SysRolePowerDao.class);
            result = delByMap(dao, ids);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        } finally {
            session.close();
        }
        return result;
    }

    /**
     * 分页数据查询
     *
     * @param pageIndex
     * @param pageSize
     * @param map
     * @return
     */
    @Override
    public  ReturnResult selectByMap(int pageIndex, int pageSize, Map<String, Object> map) {
        SqlSession session = getSqlSession();
        SysRolePowerDao dao = session.getMapper(SysRolePowerDao.class);
        int recordNum = (pageIndex - 1) * pageSize;
        List<SysRolePower> lst = session.selectList(ConfigUtils.getInstance().getDaoPackage(".SysRolePowerDao.selectByMap"), map, new RowBounds(recordNum, pageSize));
        int total = dao.count(map);
        ReturnResult result = new ReturnResult();
        result.setMsg(i18nUtils.getVal("100001000"));
        result.setRows(lst);
        result.setTotal(total);
        result.setStatus(Status.SUCCESS);
        session.close();
        return result;
    }

    @Override
    public ReturnResult insertByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        SysRolePowerDao dao = session.getMapper(SysRolePowerDao.class);
        ReturnResult result = insertByModel(dao, model);
        session.close();
        return result;
    }



    @Override
    public ReturnResult editByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        SysRolePowerDao dao = session.getMapper(SysRolePowerDao.class);
        ReturnResult result = editByModel(dao, model);
        session.close();
        return result;
    }

    /**
     * 根据ID获取一条数据
     *
     * @param id
     * @return
     */
    public ReturnResult selectById(String id) {
        SqlSession session = getSqlSession();
        ReturnResult result = new ReturnResult();
        result.setStatus(Status.ERROR);
        BaseDao dao = session.getMapper(SysRolePowerDao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        List<SysRolePower> lst = dao.selectByMap(map);
        if (lst.size() == 1) {
            result.setData(lst.get(0));
            result.setStatus(Status.SUCCESS);
        } else {
            result.setMsg(i18nUtils.getVal("100004000"));
        }
        session.close();
        return result;
    }

    /**
     * 返回当前角色拥有的权限
     *
     * @param roleId 用户ID
     * @return 返回当前用户拥有的权限
     */
    @Override
    public ReturnResult rolePower(String roleId) {
        ReturnResult result = new ReturnResult();
        SqlSession session = getSqlSession();
        SysRolePowerDao dao = session.getMapper(SysRolePowerDao.class);
        SysRolePower entity = new SysRolePower();
        entity.setRoleId(roleId);
        List<SysRolePower> lst = dao.selectByModel(entity);
        result.setStatus(Status.SUCCESS);
        result.setRows(lst);
        session.close();
        return result;
    }

    /**
     * 查询所有角色的操作权限
     *
     * @param idlst
     * @return
     */
    public ReturnResult rolesPower(List<Integer> idlst) {
        ReturnResult result = new ReturnResult();
        SqlSession session = getSqlSession();
        SysRolePowerDao dao = session.getMapper(SysRolePowerDao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roleIdArr", idlst);
        List<SysRolePower> lst = dao.selectByMap(map);
        result.setStatus(Status.SUCCESS);
        result.setRows(lst);
        session.close();
        return result;
    }
}

